package com.allen.study.domain.utils.security;

import com.allen.study.domain.entity.EmployeeLogin;
import com.allen.study.domain.repository.IEmployeeLoginRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * @ClassName: MyUserDetailsService
 * @Author: AllenSun
 * @Date: 2025/3/6 22:09
 */
@Component
public class MyUserDetailsService implements UserDetailsService {
    /*
     *  UserDetailsService：提供查询用户功能，如根据用户名查询用户，并返回UserDetails
     *  UserDetails，SpringSecurity定义的类， 记录用户信息，如用户名、密码、权限等
     * */
    @Autowired
    private IEmployeeLoginRepo employeeLoginRepo;

    @Override
    public UserDetails loadUserByUsername(String employeeId) throws UsernameNotFoundException {
        //根据用户名从数据库中查询用户
        EmployeeLogin employeeLogin = employeeLoginRepo.queryByEmployeeId(employeeId);
        if (employeeLogin==null){
            throw new UsernameNotFoundException("用户不存在");
        }
        // 封装查询到的用户信息
        MySysUserDetails mySysUserDetails=new MySysUserDetails(employeeLogin);
        return mySysUserDetails;
    }
}
